#include "SimpleSky.h"

SimpleSky::SimpleSky()
{
	_shader = std::make_shared<Shader>("sky-vert.js", "sky-frag.js");

	//sky verts
	float skyVertices[] = {
	-1.0f, 1.0f, 0.0f,
	 1.0f,-1.0f, 0.0f,
	 1.0f, 1.0f, 0.0f,
	-1.0f, 1.0f, 0.0f,
	-1.0f,-1.0f, 0.0f,
	 1.0f, -1.0f, 0.0f
	};


	GLuint sky_buffer = 0;
	glGenBuffers(1, &sky_buffer);
	glBindBuffer(GL_ARRAY_BUFFER, sky_buffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(skyVertices), skyVertices, GL_STATIC_DRAW);
	glGenVertexArrays(1, &_vao);
	glBindVertexArray(_vao);
	glBindBuffer(GL_ARRAY_BUFFER, sky_buffer);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);
	glBindVertexArray(0);
}
void SimpleSky::update(
	glm::mat4& matProj,
	glm::mat4& matView,
	glm::mat4& model,
	float nearZ,
	glm::vec3& eyepos,
	glm::vec3& gnd2sun,
	glm::mat4& inverseProjView)
{
	_matProj = &matProj;
	_matView = &matView;
	_model   = &model;
	_inverspv = &inverseProjView;
	_eyepos = eyepos ;
	_gnd2sun = gnd2sun ;
	_nearZ = nearZ ;


}
void SimpleSky::render()
{
	_shader->use();
	_shader->setFloat("u_near", _nearZ);
	_shader->setMat4("u_view", *_matView);
	_shader->setMat4("u_proj", *_matProj);
	_shader->setMat4("u_model", *_model);
	_shader->setVec3("u_eyepos", _eyepos);
	_shader->setVec3("u_earth2sun", _gnd2sun );
	_shader->setMat4("u_invtprojview", *_inverspv);
	glEnableVertexAttribArray(_vao);
	glBindVertexArray(_vao);
	glDrawArrays(GL_TRIANGLES, 0, 6);
	glEnableVertexAttribArray(0);
	_shader->unuse();
}